<?php 
defined('RUN_KATANA') or die('Restricted access');
class ScheduleItem extends Object
{
	public $ShipId;
	public $Country;
	public $CountryName;
	public $Port;
	public $IsCurrent;
	
	public $Eta;
	public $EtaStamp;
	public $EtaDate;
	public $EtaYear;
	public $EtaMonth;
	public $EtaDay;
	public $EtaTime;
	public $EtaHour;
	public $EtaMinute;
	
	public $Etd;
	public $EtdStamp;
	public $EtdDate;
	public $EtdYear;
	public $EtdMonth;
	public $EtdDay;
	public $EtdTime;
	public $EtdHour;
	public $EtdMinute;
	
	public $Archived;
	
	public function getShipId() { return $this->ShipId; }
	public function getCountry() { return $this->Country; }
	public function getCountryName() { return $this->CountryName; }
	public function getPort() { return $this->Port; }
	public function getIsCurrent() { return $this->IsCurrent; }
	
	public function getEta() { return $this->Eta; }
	public function getEtd() { return $this->Etd; }
	public function getEtaStamp() { return $this->EtaStamp; }
	public function getEtdStamp() { return $this->EtdStamp; }
	
	public function getEtaDate() { return $this->EtaDate; }
	public function getEtaYear() { return $this->EtaYear; }
	public function getEtaMonth() { return $this->EtaMonth; }
	public function getEtaDay() { return $this->EtaDay; }
	public function getEtaTime() { return $this->EtaTime; }
	public function getEtaHour() { return $this->EtaHour; }
	public function getEtaMinute() { return $this->EtaMinute; }
	
	
	public function getEtdDate() { return $this->EtdDate; }
	public function getEtdYear() { return $this->EtdYear; }
	public function getEtdMonth() { return $this->EtdMonth; }
	public function getEtdDay() { return $this->EtdDay; }
	public function getEtdTime() { return $this->EtdTime; }
	public function getEtdHour() { return $this->EtdHour; }
	public function getEtdMinute() { return $this->EtdMinute; }
	
	public function getArchived() { return $this->Archived; }
	
	
	public function setShipId($val) { $this->ShipId=$val; }
	public function setCountry($val) { $this->Country=$val; }
	public function setCountryName($val) { $this->CountryName=$val; }
	public function setPort($val) { $this->Port=$val; }
	public function setIsCurrent($val) { $this->IsCurrent=$val; }
	
	public function setEta($val) { $this->Eta=$val; self::populateEtaValues(); }
	public function setEtd($val) { $this->Etd=$val; self::populateEtdValues(); }
	
	public function setEtaStamp($val) { $this->EtaStamp=$val; }
	public function setEtdStamp($val) { $this->EtdStamp=$val; }
	
	public function setEtaDate($val) { $this->EtaDate=$val; }
	public function setEtaYear($val) { $this->EtaYear=$val; }
	public function setEtaMonth($val) { $this->EtaMonth=$val; }
	public function setEtaDay($val) { $this->EtaDay=$val; }
	public function setEtaTime($val) { $this->EtaTime=$val; }
	public function setEtaHour($val) { $this->EtaHour=$val; }
	public function setEtaMinute($val) { $this->EtaMinute=$val; }
	
	public function setEtdDate($val) { $this->EtdDate=$val; }
	public function setEtdYear($val) { $this->EtdYear=$val; }
	public function setEtdMonth($val) { $this->EtdMonth=$val; }
	public function setEtdDay($val) { $this->EtdDay=$val; }
	public function setEtdTime($val) { $this->EtdTime=$val; }
	public function setEtdHour($val) { $this->EtdHour=$val; }
	public function setEtdMinute($val) { $this->EtdMinute=$val; }
	
	public function setArchived($val) { $this->Archived=$val; }
	
	
	public function populateEtaValues()
	{
		if((isset($this->Eta)) && ($this->Eta != '') && (strlen($this->Eta) > 15))
		{
			self::setEtaYear(self::dateTimeExtract($this->Eta, 'year'));
			self::setEtaMonth(self::dateTimeExtract($this->Eta, 'month'));
			self::setEtaDay(self::dateTimeExtract($this->Eta, 'day'));
			self::setEtaHour(self::dateTimeExtract($this->Eta, 'hour'));
			self::setEtaMinute(self::dateTimeExtract($this->Eta, 'minute'));
			self::setEtaDate(substr($this->Eta, 0, 10));
			self::setEtaTime(substr($this->Eta, 11, 5));
			//mktime(hour,minute,second,month,day,year,is_dst) 
			self::setEtaStamp(mktime(self::getEtaHour(), self::getEtaMinute(), 0, self::getEtaMonth(), self::getEtaDay(), self::getEtaYear()));
		}
	}
	
	public function populateEtdValues()
	{
		if((isset($this->Etd)) && ($this->Etd != '') && (strlen($this->Etd) > 15))
		{
			self::setEtdYear(self::dateTimeExtract($this->Etd, 'year'));
			self::setEtdMonth(self::dateTimeExtract($this->Etd, 'month'));
			self::setEtdDay(self::dateTimeExtract($this->Etd, 'day'));
			self::setEtdHour(self::dateTimeExtract($this->Etd, 'hour'));
			self::setEtdMinute(self::dateTimeExtract($this->Etd, 'minute'));
			self::setEtdDate(substr($this->Etd, 0, 10));
			self::setEtdTime(substr($this->Etd, 11, 5));
			//mktime(hour,minute,second,month,day,year,is_dst) 
			self::setEtdStamp(mktime(self::getEtdHour(), self::getEtdMinute(), 0, self::getEtdMonth(), self::getEtdDay(), self::getEtdYear()));
		}
	}
	
	public function changeEtaValues()
	{
		if((isset($this->EtaDate)) && (intval($this->EtaHour) > 0) && (intval($this->EtaMinute) > 0))
		{
			$yr = self::dateTimeExtract($this->EtaDate, 'year');
			$mn = self::dateTimeExtract($this->EtaDate, 'month');
			$dy = self::dateTimeExtract($this->EtaDate, 'day');
			
			if(($yr > 0) && ($mn > 0) && ($dy > 0))
			{ 
				$time = mktime(intval($this->EtaHour), intval($this->EtaMinute), 0, $mn, $dy,  $yr);
				self::setEta(date("Y-m-d H:i:s",$time));
				self::populateEtaValues();
			}
		}
	}
	
	public function changeEtdValues()
	{
		if((isset($this->EtdDate)) && (intval($this->EtdHour) > 0) && (intval($this->EtdMinute) > 0))
		{
			$yr = self::dateTimeExtract($this->EtdDate, 'year');
			$mn = self::dateTimeExtract($this->EtdDate, 'month');
			$dy = self::dateTimeExtract($this->EtdDate, 'day');
			
			if(($yr > 0) && ($mn > 0) && ($dy > 0))
			{ 
				$time = mktime(intval($this->EtdHour), intval($this->EtdMinute), 0, $mn, $dy,  $yr);
				self::setEtd(date("Y-m-d H:i:s",$time));
				self::populateEtdValues();
			}
		}
	}
	
	public function update()
	{
		if(($this->Id > 0) && ($this->DataChanged == true))
		{
			$sql  = 'UPDATE '.DB_PERFIX.'fleet_ship_schedule SET ';
			$sql .= '`port`="'.$this->Port.'", ';
			$sql .= '`country`="'.$this->Country.'", '; 
			$sql .= '`eta`="'.$this->EtaDate.' '.$this->EtaTime.':00", '; 
			$sql .= '`etd`="'.$this->EtdDate.' '.$this->EtdTime.':00", '; 
			$sql .= '`archived`='.$this->Archived.' ';
			$sql .= 'WHERE `id`='.$this->Id.';';
			$result = mysql_query($sql);
		}
	}
}
?>